<?php
class Hydrum_Application_Resource_ModelManagers extends Zend_Application_Resource_ResourceAbstract
{

    protected $_modules;
    protected $_connections;

    public function init()
    {
        $bootstrap = $this->getBootstrap();
        $bootstrap->bootstrap(array(
            'modules',
            'database-connections'
        ));

        $this->_modules = $bootstrap->getResource('modules');
        $this->_connections = $bootstrap->getResource('database-connections');

        $managers = new Hydrum_Model_Manager_Collection();

        foreach ($this->getOptions() as $index => $options) {

            $manager = $this->createManager($options);

            $managers->set($index, $manager);
            
            if(isset($options['default']) && $options['default'])
            {
                $managers->setDefault($manager);
            }
        }

        return $managers;
    }

    protected function createManager(array $options)
    {
        $type = $options['type'];
        $method = 'create' . $type . 'Manager';

        $connection = $this->$method($options);

        return $connection;
    }

    protected function getModuleMetaPaths($type)
    {
        $paths = array();

        foreach ($this->_modules as $code => $module) {

            $founded = $module
                ->getPathfinder()
                    ->find('Model/' . $type . '/meta')
                        ->toArray();

            $paths = array_merge($paths, $founded);
        }

        return $paths;
    }

    protected function getConnection($index)
    {
        return $this->_connections[$index];
    }
    
    protected function getProxiesPath()
    {
        return APPLICATION_TMP_PATH . 'doctrine' . DS . 'proxies';
    }

    protected function createMongoDBManager(array $options)
    {
        $connection = $this->getConnection($options['connection']);
        $type = $options['type'];
        
        $config = new Doctrine\ODM\MongoDB\Configuration();
        
        $proxiesPath = $this->getProxiesPath();
        
        $config->setProxyDir($proxiesPath);
        $config->setProxyNamespace('Proxies/' . $type);
        
        $paths = $this->getModuleMetaPaths($type);        
        $driver = new Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver($paths);
        $config->setMetadataDriverImpl($driver);
        
        return Doctrine\ODM\MongoDB\DocumentManager::create($connection, $config);
    }

    protected function createPDOManager(array $options)
    {
        $connection = $this->getConnection($options['connection']);
        $type = $options['type'];
        
        $config = new Doctrine\ORM\Configuration();
        
        $proxiesPath = $this->getProxiesPath();
        
        $config->setProxyDir($proxiesPath);
        $config->setProxyNamespace('Proxies/' . $type);
        
        $paths = $this->getModuleMetaPaths($type);   
        $driver = new Doctrine\ORM\Mapping\Driver\XmlDriver($paths);
        $config->setMetadataDriverImpl($driver);
        
        return Doctrine\ORM\EntityManager::create($connection, $config);
    }
}
